import { IProvider } from "./types/data.interface"
import snail from './util/core'
import { PostHeaderType  } from './types/data.enum'
import  _ from 'lodash'
import { handlerGenRequestFun, handlerGetRequestSetConfig, handlerPostRequestSetConfig, handlerRequestSetConfig } from './util/tool'

/**
 * 请求提供者
 */
export const Provider = (providerInstance: IProvider)=>{
    return (target: any) => {
        snail.setProvider(target.name, providerInstance)
    }
}

export const BaseUrl = (url: string) =>  {
    return (target: any, propertyKey?: string,  descriptor?: PropertyDescriptor) => {
        if(target.name) {
            /**类装饰器 */
            snail.setBaseUrl(target.name, url)
        } else {
            /**方法装饰器 */
            const configMap = snail.getConfig(target.constructor.name)  || new Map()
            let  tmp = configMap.get(propertyKey) || {}
            tmp =  _.merge(tmp, {
                snail_base_url: url
            })
            configMap.set(propertyKey, tmp)
            snail.setConfig(target.constructor.name, configMap)
        }
    }
}
/**设置下载文件 */
export const FileDownLoad = (target: any, propertyKey: string,  descriptor: PropertyDescriptor) => {
    const configMap = snail.getConfig(target.constructor.name)  || new Map()
    let  tmp = configMap.get(propertyKey) || {}
    tmp =  _.merge(tmp, {
        responseType: 'blob'
    })
    configMap.set(propertyKey, tmp)
    snail.setConfig(target.constructor.name, configMap)
}

/**
 * post 请求 application/x-www-form-urlencoded
 */
export const Post = (url: string) => {
    return (target: any, propertyKey: string,  descriptor: PropertyDescriptor) => {
        const key = target.constructor.name

        handlerPostRequestSetConfig(snail, key, propertyKey,  url, PostHeaderType.normal)

        handlerGenRequestFun(snail, key, propertyKey, descriptor, PostHeaderType.normal)
    }
}

/**
 * post 请求 application/json
 */
export const PostJson = (url: string) => {
    return (target: any, propertyKey: string,  descriptor: PropertyDescriptor) => {
        const key = target.constructor.name

        handlerPostRequestSetConfig(snail, key, propertyKey,  url, PostHeaderType.json)

        handlerGenRequestFun(snail, key, propertyKey, descriptor, PostHeaderType.json)
    }
}

/**
 * post 请求 multipart/form-data
 */
export const PostFormData = (url: string)=> {
    return (target: any, propertyKey: string,  descriptor: PropertyDescriptor) => {
        const key = target.constructor.name

        handlerPostRequestSetConfig(snail, key, propertyKey,  url, PostHeaderType.formData)

        handlerGenRequestFun(snail, key, propertyKey, descriptor, PostHeaderType.formData)
    }
}


/**
 * get 请求
 */
export const Get = (url: string) => {
    return (target: any, propertyKey: string,  descriptor: PropertyDescriptor) => {
        const key = target.constructor.name 
        handlerGetRequestSetConfig(snail, key, propertyKey,  url)
        handlerGenRequestFun(snail, key, propertyKey, descriptor)
    }
}